package com.dms.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dms.common.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 统一基础服务实现类
 * @param <M> Mapper类型
 * @param <T> 实体类型
 */
@Slf4j
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {
    
    @Override
    public IPage<T> pageWithCondition(Page<T> page, Map<String, Object> conditions) {
        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
        // 构建查询条件
        buildQueryWrapper(wrapper, conditions);
        return page(page, wrapper);
    }
    
    @Override
    public List<T> listWithCondition(Map<String, Object> conditions) {
        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
        // 构建查询条件
        buildQueryWrapper(wrapper, conditions);
        return list(wrapper);
    }
    
    @Override
    public T getDetailById(Long id) {
        return getById(id);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean create(T entity) {
        return save(entity);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(T entity) {
        return updateById(entity);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return removeById(id);
    }
    
    /**
     * 构建查询条件
     * @param wrapper 查询包装器
     * @param conditions 查询条件
     */
    protected abstract void buildQueryWrapper(LambdaQueryWrapper<T> wrapper, Map<String, Object> conditions);
} 